package com.ai.web.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ai.web.service.RepairService;
import com.ai.web.util.WorkStatus;


@Controller
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {
	
	private static Logger logger = Logger.getLogger(StatisticsController.class);
	//统计天数
	private static final int STAT_DATE_NUM = 7;

	@Autowired
	private RepairService repairService;
	
	@RequestMapping(value = "/repair")
	public String repair(Model model) {
		
		String dateDateStr = "";
		String[] data = new String[8];
		
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");	
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -6);
		List<WorkStatus> list = WorkStatus.getList();
		int count=0;
		while(count<STAT_DATE_NUM) {
			String key = sd.format(calendar.getTime());
			dateDateStr += key+",";
			Map<Byte,Integer> map = statByDate(key);
			
			//赋值			
			for(WorkStatus w : list) {
				data[w.getValue()] += map.get(w.getValue())+",";
			}
			
			if(logger.isDebugEnabled()) {
				logger.info("listMap size is "+map.size());
			}
			count++;
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		dateDateStr = dateDateStr.substring(0,dateDateStr.length()-1);
		for(WorkStatus w : list) {
			data[w.getValue()] = data[w.getValue()].substring(0,data[w.getValue()].length()-1);
		}
		
		if(logger.isDebugEnabled()) {
			logger.debug("横轴时间串:"+dateDateStr);
			for(WorkStatus w : list) {
				logger.debug("data"+w.getValue()+":"+data[w.getValue()]);
			}
		}
		
		model.addAttribute("dateDateStr", dateDateStr);
		for(WorkStatus w : list) {
			model.addAttribute("data"+w.getValue(), data[w.getValue()]);
		}
		return "statistics/statistics-repair";
	}
	/**
	 * 
	 * @param key 日期
	 * @return
	 */
	private Map<Byte,Integer> statByDate(String dateKey) {

		Map<Byte,Integer> statusMap = new HashMap<Byte,Integer>();
		List<Map<String, Object>> results = repairService.getCountByStatus(dateKey);
		
		for (Map<String, Object> map : results) {
			Byte status = MapUtils.getByte(map, "status");
			Integer count = MapUtils.getIntValue(map, dateKey);
			if(logger.isDebugEnabled()) {
				logger.debug("status:"+status+"---count:"+count);
			}
			statusMap.put(status, count);
		}
		
		//补零
		List<WorkStatus> list = WorkStatus.getList();
		for(WorkStatus w : list) {
			if(statusMap.get(w.getValue())==null) {
				statusMap.put(w.getValue(), 0);
			}
		}
		
		return statusMap;
	}
	@RequestMapping(value = "/user")
	public String user(Model model) {
		return "statistics/statistics-user";
	}

	@RequestMapping(value = "/comment")
	public String comment(Model model) {
		return "statistics/statistics-comment";
	}
	
}
